<template>
    <div class="goobody">
      <div>
        <div class="nav-left" ref="navleft" >
          <ul>
            <li :class="a===index?'bgwr':''" @click="change(index)" v-for="(item,index) in arrgoods" :key="index">{{item.name}}</li>
          </ul>
        </div>
        <div class="bodygood" ref="navcon" >
          <div>
            <ul v-for="(item1,index1) in arrgoods" :key="index1" ref="lostli">
              <li class="goodtitle" >{{item1.name}}</li>
              <li  v-for="(item2,index2) in item1.foods" :key="index2" class="goodcon">
                <img @click="fn(item2)" :src="item2.image" alt="">
                <div>
                  <h2 @click="fn(item2)">{{item2.name}}</h2>
                  <p>{{item2.description}}</p>
                  <p>月售{{item2.sellCount}}份  好评{{item2.rating}}%</p>
                  <span>￥{{item2.price}}</span><del>￥{{item2.oldPrice}}</del>
                </div>
                <section><v-num :food="item2"></v-num></section>
                <hr style="margin: 0;margin-top: 0.3rem; ">
              </li>
            </ul>
          </div>
        </div>
      </div>
      <v-det :onfood="xq"  ref="det" v-if="boo" @gfa="fon"></v-det>
    </div>
</template>

<script>
import vNum from './thenum.vue'
import Better from 'better-scroll'
import vDet from './detail.vue'
export default {
  name: 'goo',
  data () {
    return {
      // arrgoods: '',
      a: 0,
      allhgt: [0],
      scrollY: '',
      xq: '',
      boo: false
    }
  },
  computed: {
    arrgoods () {
      return this.$store.state.count.goods
    }
  },
  components: {
    vNum,
    vDet
  },
  methods: {
    // 详情页
    fn (val) {
      this.xq = val
      this.boo = true
      // this.$nextTick(() => {
      //   this.$refs.det.show()
      // })
    },
    fon (val) {
      this.boo = val
    },
    change (index) {
      this.a = index
      this.oright.scrollToElement(this.$refs.lostli[index], 100, 0, 0)
    },
    getHei () {
      // console.log(this.$refs.lostli)
      this.$refs.lostli.forEach((item, index) => {
        // console.log(item.clientHeight)
        this.allhgt.push(item.clientHeight + this.allhgt[index])
      })
    },
    scroll () {
      for (let i = 0; i < this.allhgt.length; i++) {
        if (this.allhgt[i] < this.scrollY && this.allhgt[i + 1] > this.scrollY) {
          this.a = i
        }
      }
    }
  },
  created () {
    // setTimeout(() => {
    //   this.arrgoods = this.$store.state.count.goods
    //   // console.log(this.$store.state.count.goods)
    // }, 100)
  },
  mounted () {
    setTimeout(() => {
      this.oleft = new Better(this.$refs.navleft, {
        click: true
      })
      this.oright = new Better(this.$refs.navcon, {
        click: true,
        probeType: 2
      })
      this.oright.on('scroll', (res) => {
        this.scrollY = Math.abs(Math.ceil(res.y))
        this.scroll()
      })
      this.getHei()
    }, 100)
  }
}

</script>

<style scoped lang="less">
  @color:rgba(7,17,27,0.1);
.goobody{
  .nav-left{
    overflow: hidden;
    position: fixed;
    top: 0;
    bottom: 0;
    margin-top: 3.48rem;
    margin-bottom: 1rem;
    box-sizing: border-box;
    font-size: 0.24rem;
    font-weight: 200;
    ul{
      li{
        position: relative;
        height: 1.08rem;
        width: 1.60rem;
        text-align: center;
        background: #f3f5f7;
        padding: 0.4rem 0.24rem;
        box-sizing: border-box;
        line-height: 0.28rem;
        &:after{
          content:"";
          display: inline-block;
          width: 1.12rem;
          height: 0.02rem;
          background: rgba(7,17,27,0.1);
          position: absolute;
          bottom: 0rem;
          left: 50%;
          transform: translateX(-50%);
        }
      }
    }
  }
  .bodygood{
    position: fixed;
    left: 1.6rem;
    top: 0;
    bottom: 0;
    box-sizing: border-box;
    margin-top: 3.48rem;
    margin-bottom: 1rem;
    overflow: hidden;
    width: 5.9rem;
    .goodcon{
      position: relative;
      padding: 0.36rem;
      padding-bottom: 0;
      img{
        float: left;
        width: 1.2rem;
        height: 1.2rem;
      }
      section{
        position: absolute;
        width: 1.32rem;
        height: 0.36rem;
        right: 0.36rem;
        bottom: 0.36rem;
      }
      &>div{
        margin-top: 0.04rem;
        margin-left: 1.4rem;
        h2{
          font-size: 0.28rem;
          color: rgb(7,17,27);
          line-height: 0.28rem;
        }
        p{
          margin-top: 0.16rem;
          font-size: 0.2rem;
          color: rgb(147,153,149);
          line-height: 0.2rem;
        }
        span{
          margin-top: 0rem;
          margin-right: 0.16rem;
          color: red;
          font-size: 0.28rem;
          font-weight: 700;
          line-height: 0.48rem;
        }
        del{
          font-weight: 700;
          line-height: 0.48rem;
          font-size: 0.2rem;
        }
      }
    }
    .goodtitle{
      height: 0.52rem;
      font-size: 0.20rem;
      line-height: 0.52rem;
      background-color: @color;
      color: rgb(147,153,159);
      border-left: 0.05rem solid darkgray;
      padding-left: 0.2rem;
    }
  }
  .bgwr{
    background-color: white!important;
    font-size: 0.24rem!important;
  }
}
</style>
